import { defineStore } from "pinia";
import { ref, computed } from "vue";

export interface ExperimentCategory {
  id: string;
  name: string;
  icon: string;
}

export interface Experiment {
  id: string;
  name: string;
  route: string;
  categoryId: string;
  icon?: string;
  description?: string;
  isImplemented?: boolean;
}

export const useExperimentStore = defineStore("experiment", () => {
  // State
  const categories = ref<ExperimentCategory[]>([
    { id: "optics", name: "光学实验", icon: "🔍" },
    { id: "electricity", name: "电学实验", icon: "⚡" },
    { id: "mechanics", name: "力学实验", icon: "🎯" },
    { id: "thermodynamics", name: "热学实验", icon: "🌡️" },
    { id: "modern", name: "现代物理实验", icon: "⚛️" },
    { id: "comprehensive", name: "综合实验", icon: "🔬" },
    { id: "dev", name: "开发文档", icon: "📚" },
  ]);

  const experiments = ref<Experiment[]>([
    // 光学实验
    {
      id: "spectrometer",
      name: "分光计实验",
      route: "spectrometer",
      categoryId: "optics",
      icon: "🔍",
      description:
        "通过分光计测量不同光谱线的衍射角，计算光的波长并与标准值进行对比分析",
      isImplemented: true,
    },
    {
      id: "fiber-optic-communication",
      name: "光纤通信实验",
      route: "fiber-optic-communication",
      categoryId: "optics",
      icon: "📡",
      description: "学习光纤通信的基本原理，测量光信号在光纤中的传输特性",
      isImplemented: false,
    },
    {
      id: "thin-film-refraction",
      name: "测量透明薄片的折射率",
      route: "thin-film-refraction",
      categoryId: "optics",
      icon: "🔎",
      description: "使用干涉方法测量透明薄膜的折射率，理解光的干涉原理",
      isImplemented: false,
    },
    {
      id: "fiber-sensor",
      name: "光纤传感器实验",
      route: "fiber-sensor",
      categoryId: "optics",
      icon: "🎚️",
      description: "探究光纤传感器的工作原理，测量不同物理量对光信号的影响",
      isImplemented: false,
    },
    {
      id: "polarization",
      name: "光的偏振实验",
      route: "polarization",
      categoryId: "optics",
      icon: "↔️",
      description: "研究光的偏振特性，验证马吕斯定律，了解偏振光的应用",
      isImplemented: false,
    },
    {
      id: "michelson-interferometer",
      name: "迈克尔逊干涉仪",
      route: "michelson-interferometer",
      categoryId: "optics",
      icon: "🔆",
      description: "使用迈克尔逊干涉仪测量波长和细小距离，了解光的干涉原理",
      isImplemented: false,
    },
    {
      id: "telescope-microscope",
      name: "自组望远镜和显微镜",
      route: "telescope-microscope",
      categoryId: "optics",
      icon: "🔭",
      description: "了解光学仪器的基本结构和原理，自行组装简单的望远镜和显微镜",
      isImplemented: false,
    },
    {
      id: "equal-thickness-interference",
      name: "等厚干涉实验",
      route: "equal-thickness-interference",
      categoryId: "optics",
      icon: "🌈",
      description: "观察和测量等厚薄膜的干涉条纹，理解等厚干涉原理",
      isImplemented: false,
    },
    {
      id: "lens-focal-length",
      name: "薄透镜焦距的测定",
      route: "lens-focal-length",
      categoryId: "optics",
      icon: "👁️",
      description: "使用多种方法测量凸透镜和凹透镜的焦距，理解成像原理",
      isImplemented: false,
    },

    // 电学实验
    {
      id: "potentiometer",
      name: "电位差计实验",
      route: "potentiometer",
      categoryId: "electricity",
      icon: "🔋",
      description: "学习电位差计的工作原理，测量电动势和内阻",
      isImplemented: false,
    },
    {
      id: "bridge",
      name: "自组电桥测电阻实验",
      route: "bridge",
      categoryId: "electricity",
      icon: "⚡",
      description:
        "通过电桥原理测量电阻值，分析平衡条件和误差来源，掌握电阻测量的基本方法",
      isImplemented: true,
    },
    {
      id: "electron-beam",
      name: "电子束的偏转和电子比荷的测定",
      route: "electron-beam",
      categoryId: "electricity",
      icon: "💫",
      description: "研究电子在电场和磁场中的运动规律，测定电子的比荷",
      isImplemented: false,
    },
    {
      id: "electrostatic-field",
      name: "用模拟法测绘静电场实验",
      route: "electrostatic-field",
      categoryId: "electricity",
      icon: "⚪",
      description: "使用导电纸模拟静电场，测绘等势线，理解静电场的性质",
      isImplemented: false,
    },
    {
      id: "magnetic-hysteresis",
      name: "动态磁滞回线的测量",
      route: "magnetic-hysteresis",
      categoryId: "electricity",
      icon: "🧲",
      description: "测量铁磁材料的磁滞回线，了解铁磁材料的磁化特性",
      isImplemented: false,
    },
    {
      id: "magnetic-field-measurement",
      name: "磁场的测量",
      route: "magnetic-field-measurement",
      categoryId: "electricity",
      icon: "📊",
      description: "使用霍尔元件测量磁场强度，了解磁场的空间分布",
      isImplemented: false,
    },
    {
      id: "meter-modification",
      name: "电表的改装和校准",
      route: "meter-modification",
      categoryId: "electricity",
      icon: "🔌",
      description: "学习电表的工作原理，实践电表的改装和校准方法",
      isImplemented: false,
    },

    // 力学实验
    {
      id: "youngs-modulus",
      name: "拉伸法测金属丝的杨氏模量",
      route: "youngs-modulus",
      categoryId: "mechanics",
      icon: "🔩",
      description: "通过测量金属丝的拉伸变形，计算其杨氏模量",
      isImplemented: false,
    },
    {
      id: "forced-vibration",
      name: "受迫振动实验",
      route: "forced-vibration",
      categoryId: "mechanics",
      icon: "🔊",
      description: "研究振动系统在外力作用下的振动规律，测量共振曲线",
      isImplemented: false,
    },
    {
      id: "moment-of-inertia",
      name: "测量物体的转动惯量",
      route: "moment-of-inertia",
      categoryId: "mechanics",
      icon: "🔄",
      description: "使用扭摆法测量不同物体的转动惯量，理解角动量守恒",
      isImplemented: false,
    },
    {
      id: "kinematics",
      name: "运动学实验",
      route: "kinematics",
      categoryId: "mechanics",
      icon: "🚀",
      description: "研究物体的直线运动和曲线运动，验证牛顿运动定律",
      isImplemented: false,
    },
    {
      id: "pendulum-friction",
      name: "单摆落体与摩擦系数",
      route: "pendulum-friction",
      categoryId: "mechanics",
      icon: "⏱️",
      description: "通过单摆和落体实验，测量重力加速度和摩擦系数",
      isImplemented: false,
    },

    // 热学实验
    {
      id: "viscosity-temperature",
      name: "液体变温粘滞系数的测定",
      route: "viscosity-temperature",
      categoryId: "thermodynamics",
      icon: "💧",
      description: "研究液体粘滞系数随温度的变化规律，验证相关理论",
      isImplemented: false,
    },
    {
      id: "ideal-gas-law",
      name: "理想气体定律实验",
      route: "ideal-gas-law",
      categoryId: "thermodynamics",
      icon: "☁️",
      description: "验证理想气体状态方程，研究气体的热力学性质",
      isImplemented: false,
    },
    {
      id: "heat-engine",
      name: "热机实验",
      route: "heat-engine",
      categoryId: "thermodynamics",
      icon: "🔥",
      description: "研究热机的工作原理和热效率，验证热力学第二定律",
      isImplemented: false,
    },

    // 现代物理实验
    {
      id: "franck-hertz",
      name: "弗兰克-赫兹实验",
      route: "franck-hertz",
      categoryId: "modern",
      icon: "⚛️",
      description: "验证原子能级的量子化，测量汞原子的第一激发能",
      isImplemented: false,
    },
    {
      id: "millikan",
      name: "密立根油滴实验",
      route: "millikan",
      categoryId: "modern",
      icon: "💧",
      description: "测量电子的电荷量，验证电荷的量子化性质",
      isImplemented: false,
    },
    {
      id: "electron-emission",
      name: "测定金属电子的逸出功与荷质比",
      route: "electron-emission",
      categoryId: "modern",
      icon: "🔬",
      description: "测量金属电子的逸出功和电子的荷质比，验证光电效应理论",
      isImplemented: false,
    },
    {
      id: "photoelectric-effect",
      name: "光电效应实验",
      route: "photoelectric-effect",
      categoryId: "modern",
      icon: "💡",
      description: "研究光电效应，测定普朗克常数，验证爱因斯坦光电方程",
      isImplemented: false,
    },

    // 综合实验
    {
      id: "solar-cell",
      name: "太阳能电池的特性测量实验",
      route: "solar-cell",
      categoryId: "comprehensive",
      icon: "☀️",
      description: "测量太阳能电池的伏安特性和功率特性，研究影响因素",
      isImplemented: false,
    },
    {
      id: "surface-tension",
      name: "测量液体表面张力系数",
      route: "surface-tension",
      categoryId: "comprehensive",
      icon: "💦",
      description: "使用多种方法测量液体的表面张力系数，了解表面现象",
      isImplemented: false,
    },
    {
      id: "sound-speed",
      name: "声速的测量",
      route: "sound-speed",
      categoryId: "comprehensive",
      icon: "🔊",
      description: "测量声波在不同介质中的传播速度，研究声波的性质",
      isImplemented: false,
    },
    {
      id: "rotating-liquid",
      name: "旋转液体物理特性的测量",
      route: "rotating-liquid",
      categoryId: "comprehensive",
      icon: "🌪️",
      description: "研究液体旋转时的形状和压强分布，验证流体力学原理",
      isImplemented: false,
    },
    {
      id: "temperature-alarm",
      name: "DIY温度报警器",
      route: "temperature-alarm",
      categoryId: "comprehensive",
      icon: "🚨",
      description: "设计并制作温度报警器，学习传感器和电路的基本原理",
      isImplemented: false,
    },
    {
      id: "digital-scale",
      name: "DIY数字称量计",
      route: "digital-scale",
      categoryId: "comprehensive",
      icon: "⚖️",
      description: "设计并制作数字称量计，了解力传感器和数据采集的原理",
      isImplemented: false,
    },
    {
      id: "acoustic-positioning",
      name: "声测距与定位",
      route: "acoustic-positioning",
      categoryId: "comprehensive",
      icon: "📍",
      description: "研究声波测距原理，设计声波定位系统，了解声学应用",
      isImplemented: false,
    },

    // 开发文档
    {
      id: "template-demo",
      name: "组件模板演示",
      route: "template-demo",
      categoryId: "dev",
      icon: "📝",
      description: "物理实验数据处理平台的组件开发模板和示例",
      isImplemented: true,
    },
    {
      id: "element-demo",
      name: "Element Plus 组件演示",
      route: "element-demo",
      categoryId: "dev",
      icon: "🧩",
      description: "Element Plus UI组件库的使用示例和最佳实践",
      isImplemented: true,
    },
  ]);

  const recentExperiments = ref<string[]>([]);
  const currentExperimentId = ref<string | null>(null);

  // Actions
  function setCurrentExperiment(experimentId: string) {
    currentExperimentId.value = experimentId;

    // Add to recent experiments if not already there
    if (!recentExperiments.value.includes(experimentId)) {
      // Keep only the last 5 experiments
      if (recentExperiments.value.length >= 5) {
        recentExperiments.value.pop();
      }
      recentExperiments.value.unshift(experimentId);
    }
    // If already in list, move to top
    else {
      const index = recentExperiments.value.indexOf(experimentId);
      recentExperiments.value.splice(index, 1);
      recentExperiments.value.unshift(experimentId);
    }
  }

  // Getters
  const currentExperiment = computed(() => {
    return (
      experiments.value.find((exp) => exp.id === currentExperimentId.value) ||
      null
    );
  });

  const experimentsByCategory = computed(() => {
    const result: Record<string, Experiment[]> = {};

    categories.value.forEach((category) => {
      result[category.id] = experiments.value.filter(
        (experiment) => experiment.categoryId === category.id
      );
    });

    return result;
  });

  const recentExperimentsList = computed(() => {
    return recentExperiments.value
      .map((id) => experiments.value.find((exp) => exp.id === id))
      .filter(Boolean) as Experiment[];
  });

  const implementedExperiments = computed(() => {
    return experiments.value.filter((exp) => exp.isImplemented);
  });

  return {
    categories,
    experiments,
    recentExperiments,
    currentExperimentId,
    currentExperiment,
    experimentsByCategory,
    recentExperimentsList,
    implementedExperiments,
    setCurrentExperiment,
  };
});
